package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"gosh/config"
	"gosh/pkg/login"
	"os"
	"time"
)

// loginCmd represents the login command
var loginCmd = &cobra.Command{
	Use:   "login -H host [flags]",
	Short: "login remote host then open terminal",
	Long:  `login remote host then open terminal`,
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		if err := config.LoginInfo.Parse(); err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		login.Run()
	},
}

func init() {
	rootCmd.AddCommand(loginCmd)
	loginCmd.Flags().StringVarP(&config.LoginInfo.Host, "host", "H", "", "转发请求的远程主机")
	loginCmd.Flags().IntVarP(&config.LoginInfo.Port, "port", "p", 22, "指定远程主机ssh的端口")
	loginCmd.Flags().StringVarP(&config.LoginInfo.User, "user", "u", "", "远程ssh用户,默认当前用户")
	loginCmd.Flags().StringVarP(&config.LoginInfo.Password, "password", "P", "", "远程ssh用户的密码")
	loginCmd.Flags().StringVarP(&config.LoginInfo.Key, "key", "k", "~/.ssh/id_rsa", "指定ssh私钥")
	loginCmd.Flags().StringVarP(&config.LoginInfo.KeyPassphrase, "key.passphrase", "", "", "ssh私钥的密码")
	loginCmd.Flags().StringVarP(&config.LoginInfo.BastionHost, "bastion.host", "j", "", "指定跳板机IP")
	loginCmd.Flags().IntVarP(&config.LoginInfo.BastionPort, "bastion.port", "", 22, "指定跳板机SSH端口")
	loginCmd.Flags().StringVarP(&config.LoginInfo.BastionUser, "bastion.user", "", "", "指定跳板机SSH用户,默认当前用户")
	loginCmd.Flags().StringVarP(&config.LoginInfo.BastionPassword, "bastion.password", "", "", "指定跳板机密码")
	loginCmd.Flags().StringVarP(&config.LoginInfo.BastionKey, "bastion.key", "", "~/.ssh/id_rsa", "指定跳板机ssh私钥")
	loginCmd.Flags().StringVarP(&config.LoginInfo.BastionKeyPassphrase, "bastion.key.passphrase", "", "", "指定跳板机ssh私钥的密码")
	loginCmd.Flags().DurationVarP(&config.LoginInfo.ConnectTimeout, "timeout", "t", time.Second*5, "连接超时时间,ssh连接和proxy转发超时")
	_ = loginCmd.MarkFlagRequired("host")
}
